package com.javatest.validate;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @date 2020-09-01 下午4:44
 */
public class App {
    public static void main(String[] args) {
        Person person = new Person();
        person.setAge(-1);

        // 1. 使用默认配置得到一个校验工厂，这个配置可以来自于provider、SPI提供
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        // 2. 得到一个校验器
        final Validator validator = validatorFactory.getValidator();
        // 3. 校验Java Bean（解析注解）返回校验结果
        final Set<ConstraintViolation<Person>> violations = validator.validate(person);
        // 输出校验结果
        violations.stream().map(v -> v.getPropertyPath() + " " + v.getMessage() + ": " + v.getInvalidValue()).forEach(System.out::println);
    }
}
